package com.sjzn.module_park.viewmodel

import androidx.databinding.ObservableField
import androidx.test.espresso.matcher.ViewMatchers
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.ReverseCarRequestBean
import com.czl.base.data.bean.ReverseSearchBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

class ReveseSearchCarViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {
    var carNo=ObservableField("")
    var emptyIsVisible=ObservableField(false)
    val uc=UiChangeEvent()
    val Vi= ViewMatchers.Visibility.GONE
    class UiChangeEvent{
        val loadCompleteEvent: SingleLiveEvent<ReverseSearchBean> = SingleLiveEvent()
    }

    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        reverseSearchCar()
    })
    fun reverseSearchCar() {
        if (carNo.get()?.isEmpty() == true){
            showNormalToast("请输入车牌号")
            return
        }
        var reverseCarRequestBean= ReverseCarRequestBean(model.getAreaId(),carNo.get()!!)
        model.getReverseSearchCar(reverseCarRequestBean)
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .doOnSubscribe{showLoading()}
            .subscribe(object : ApiSubscriberHelper<BaseBean<ReverseSearchBean>>(){
                override fun onResult(t: BaseBean<ReverseSearchBean>) {
                    dismissLoading()
                    if (t.code==200){
                        if (t.data?.dataItems?.isEmpty() == true){
                            emptyIsVisible.set(false)
                        }else{
                            emptyIsVisible.set(true)
                            uc.loadCompleteEvent.postValue(t.data)
                        }
                    }else{
                        emptyIsVisible.set(false)
                        uc.loadCompleteEvent.postValue(null)
                    }
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                    emptyIsVisible.set(false)
                    uc.loadCompleteEvent.postValue(null)
                }

            })
    }
}